from kivy.resources import resource_add_path as kivy_resource_add_path
from kivy.resources import resource_find

from os import path
import os
import sys


__all__ = ("resource_find", "resource_add_path")


def resource_add_path(directory, recursive=sys.maxint):
    """A replacement for kivy.resources.resource_add_path(). This has an additional argument which
    allows paths to be added recursively. If recursive is a positive integer, paths are added
    recursively up to the given number of levels. If recursive is negative, all sub-directories
    are added to kivy's resource search path. If recursive is zero, this function simply adds the
    argument directory to kivy's resource search path list."""
    print "adding resource directory:", directory
    kivy_resource_add_path(directory)
    if recursive == 0:
        return
    for entry in os.listdir(directory):
        fullpath = path.join(directory, entry)
        if path.isdir(fullpath):
            resource_add_path(fullpath, recursive - 1)
